package control;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import xml.XMLWorker;

public class Watcher {
	
	private XMLWorker worker = null;
	private Object[][] events = null;
	private SimpleDateFormat date = null;
	private List<Object[]> soonEvents = null;

	public Watcher(XMLWorker worker) {
		this.worker = worker;
		date = new SimpleDateFormat("dd.MM.yyyy");
	}

	public boolean hasEvent() {
		events = worker.getEvents();
		soonEvents = new ArrayList<Object[]>();
		for(int i=0; i<events.length; i++) {
			Date d = null;
			try {
				d = date.parse(date.format(events[i][1]));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			long millisOfThreeDays = 1000*60*60*24*3;
			GregorianCalendar now = new GregorianCalendar();
			GregorianCalendar threeDays = new GregorianCalendar();
			threeDays.setTimeInMillis(now.getTimeInMillis()+millisOfThreeDays);
			GregorianCalendar eventDate = new GregorianCalendar();
			eventDate.setTime(d);
			eventDate.set(GregorianCalendar.YEAR, now.get(GregorianCalendar.YEAR));
			if(eventDate.getTimeInMillis()>now.getTimeInMillis()
					&& eventDate.getTimeInMillis()<threeDays.getTimeInMillis()) {
				soonEvents.add(events[i]);
			}
		}
		if(soonEvents.size()==0) {
			return false;
		}
		return true;
	}

	public Object[][] getEvents() {
		Object[][] o = new Object[soonEvents.size()][4];
		for(int i=0; i<soonEvents.size();i++) {
			o[i][0] = soonEvents.get(i)[0];
			o[i][1] = soonEvents.get(i)[1];
			o[i][2] = soonEvents.get(i)[2];
			o[i][3] = soonEvents.get(i)[3];
		}
		return o;
	}

	public Object[] getTableTitles() {
		return worker.getTableTitles();
	}

}
